const { app, BrowserWindow, Menu, globalShortcut, ipcMain } = require('electron')
// { app }用来控制程序的生命周期
// { BrowserWindow }用来桌面应用窗口配置

//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------

// 修改代码自动更新
const reloader = require("electron-reloader");
reloader(module);

//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------

// 主进程打印会在cmd里面显示
// console.log(module)

//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// 当 Electron 完成初始化时，触发一次
app.on("ready", () => {
  // 设置启动窗口配置
  // 用const定义窗口实例
  const myBrowserWindow = new BrowserWindow({
    width: 800,
    height: 800,
    // 创建无边框窗口
    frame: false,
    webPreferences: {
      // 开启node模块
      nodeIntegration: true,
      contextIsolation: false,
      // 开启remote模块
      enableRemoteModule: true,
    },
  })
  require("@electron/remote/main").initialize(); // 初始化
  require("@electron/remote/main").enable(myBrowserWindow.webContents)

  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------

  // 默认打开渲染进程调试工具
  // myBrowserWindow.webContents.openDevTools();

  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------

  // 用创建的窗口实例上的loadFile方法引入需要展示的内容文件
  myBrowserWindow.loadFile('./src/index.html')

  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------

  // 菜单抽离，放到main.js里了，所以直接注释下面的代码，用require("./menu")引入
  // require("./menu") // 若设置无边框/自定义边框需要注释
  // // 定义自定义菜单
  // const template = [
  //   { label: "文件", submenu: [ { label: "新建窗口", /* 自定义菜单事件 */click: () => { const newMainWindow = new BrowserWindow({ width: 300, height: 300, }); newMainWindow.loadFile("./src/new.html"); } } ] },
  //   { label: "编辑", submenu: [ { label: "新建窗口", }, ], },
  // ]
  // const menu = Menu.buildFromTemplate(template);
  // Menu.setApplicationMenu(menu);

  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------

  // 定义快捷键
  globalShortcut.register("CommandOrControl+X", () => {
    // 主进程打印
    console.log("CommandOrControl+X is pressed + 打印结果在命令行");
  })
  // 窗口最小
  globalShortcut.register("CommandOrControl+T", () => {
    myBrowserWindow.unmaximize()
  })
  // 关闭窗口
  globalShortcut.register("CommandOrControl+H", () => {
    myBrowserWindow.close()
  })
  // 窗口最大
  globalShortcut.register("CommandOrControl+M", () => {
    myBrowserWindow.maximize()
  })

  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------------------
  
  // 监听自定义事件
  // 最大
  ipcMain.on('max-window', (event, arg) => {
    myBrowserWindow.maximize()
  })
  // 最小
  ipcMain.on('min-window', (event, arg) => {
    console.log(arg);
    myBrowserWindow.unmaximize()
  })
  // 关闭
  ipcMain.on('close-window', (event, arg) => {
    myBrowserWindow.close()
  })
})

